<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入设备名称（限25字）"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备IP" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ipAddr" >
              <a-input v-model="model.ipAddr" placeholder="请输入设备IP" :disabled="isEdit"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备端口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="port">
              <a-input-number v-model="model.port" placeholder="请输入设备端口" style="width: 100%" :disabled="isEdit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="在线状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onStatus">
              <j-dict-select-tag type="list" v-model="model.onStatus" dictCode="yn" placeholder="请选择在线状态" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="上线时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onTime">
              <j-date placeholder="请选择上线时间" v-model="model.onTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="离线时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="offTime">
              <j-date placeholder="请选择离线时间" v-model="model.offTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="12">
            <a-form-model-item label="影子名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shadowName">
              <a-textarea v-model="model.shadowName" rows="4" placeholder="请输入影子名称" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="是否激活" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ison">
              <j-dict-select-tag type="list" v-model="model.ison" dictCode="yn" placeholder="请选择是否激活" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属产品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productId">
              <j-dict-select-tag type="list" v-model="model.productId" dictCode="tig_product,name,id"
                placeholder="请选择所属产品" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属机构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sysOrgCode">
              <j-dict-select-tag type="list" v-model="model.sysOrgCode" dictCode="sys_depart,depart_name,org_code"
                placeholder="请选择所属机构" :disabled=true />
            </a-form-model-item>
          </a-col>
        </a-row>
        <!-- <a-row>
          <a-col :span="24">
            <vue-json-editor v-model="json" :show-btns="true" :expandedOnStart="false"
              @json-save="onJsonSave">保存</vue-json-editor>
          </a-col>
        </a-row> -->
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import vueJsonEditor from 'vue-json-editor'

export default {
  name: 'TigDeviceForm',
  components: {
    vueJsonEditor
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入设备名称!' },
        ],
        ison: [
          { required: true, message: '请输入是否激活!' },
        ],
        ipAddr: [
          { required: true, message: '请输入连接IP!' },
        ],
        port: [
          { required: true, message: '请输入设备连接端口号' },
        ],
        productId: [
          { required: true, message: '请输入设备所属产品' },
        ],
      },
      url: {
        add: "/tiger/tigDevice/add",
        edit: "/tiger/tigDevice/edit",
        queryById: "/tiger/tigDevice/queryById"
      },
      json: {
        msg: 'jsoneditor'
      },
      isEdit:false, //是否为编辑状态
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add(_data) {
      if (_data != undefined && _data.sysOrgCode != undefined){
        this.model = Object.assign({}, this.model);
        this.model.sysOrgCode = _data.sysOrgCode;
        this.model.onStatus = 1;
        this.model.ison = 1;
        this.productId = '1500510244894281730'
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
      }
      this.edit(this.modelDefault);
      this.isEdit = false;
    },
    edit(record) {
      this.isEdit = true;
      this.model = Object.assign({}, record);
      if (this.model.shadowName)
        this.json = JSON.parse(this.model.shadowName);
      else
        this.json = {};
      if (!isNaN(this.model.onTime) && !(this.model.onTime instanceof Date)) {
        let date = new Date(this.model.onTime);
        this.model.onTime = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
      }
      if (!isNaN(this.model.offTime) && !(this.model.offTime instanceof Date)) {
        let date = new Date(this.model.offTime);
        this.model.offTime = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
      }
      this.visible = true;
    },
    submitForm() {
      const that = this;
      if(this.model.name.length > 30){
        this.$message.info("名字超出长度")
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!isNaN(this.model.onTime) && !(this.model.onTime instanceof Date)) {
            let date = new Date(this.model.onTime);
            this.model.onTime = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
          }
          if (!isNaN(this.model.offTime) && !(this.model.offTime instanceof Date)) {
            let date = new Date(this.model.offTime);
            this.model.offTime = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
          }
          if (!isNaN(this.model.createTime) && !(this.model.createTime instanceof Date)) {
            let date = new Date(this.model.createTime);
            this.model.createTime = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
          }
          if (!isNaN(this.model.updateTime) && !(this.model.updateTime instanceof Date)) {
            let date = new Date(this.model.updateTime);
            this.model.updateTime = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
          }
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    // 点击保存时触发
    onJsonSave(value) {
      console.log('value:', value)
      this.model.shadowName = JSON.stringify(this.json)
      this.$message.info("保存成功")
    },
  }
}
</script>